/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.ejweixin.service.impl;

import com.alibaba.fastjson.JSON;
import me.zhengjie.modules.ejweixin.domain.NgConstructionSiteInfoStaff;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.ejweixin.service.NgConstructionSiteInfoStaffService;
import me.zhengjie.modules.ejweixin.domain.vo.NgConstructionSiteInfoStaffQueryCriteria;
import me.zhengjie.modules.ejweixin.mapper.NgConstructionSiteInfoStaffMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.lang.reflect.Field;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author cy
* @date 2025-04-23
**/
@Service
@RequiredArgsConstructor
public class NgConstructionSiteInfoStaffServiceImpl extends ServiceImpl<NgConstructionSiteInfoStaffMapper, NgConstructionSiteInfoStaff> implements NgConstructionSiteInfoStaffService {

    private final NgConstructionSiteInfoStaffMapper ngConstructionSiteInfoStaffMapper;

    @Override
    public PageResult<NgConstructionSiteInfoStaff> queryAll(NgConstructionSiteInfoStaffQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(ngConstructionSiteInfoStaffMapper.findAll(criteria, page));
    }

    @Override
    public List<NgConstructionSiteInfoStaff> queryAll(NgConstructionSiteInfoStaffQueryCriteria criteria){
        return ngConstructionSiteInfoStaffMapper.findAll(criteria);
    }

    @Override
    public List<NgConstructionSiteInfoStaff> staffList(int siteId) {
        return ngConstructionSiteInfoStaffMapper.staffList(siteId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(NgConstructionSiteInfoStaff resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NgConstructionSiteInfoStaff resources) {
        NgConstructionSiteInfoStaff ngConstructionSiteInfoStaff = getById(resources.getId());
        ngConstructionSiteInfoStaff.copy(resources);
        saveOrUpdate(ngConstructionSiteInfoStaff);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<NgConstructionSiteInfoStaff> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NgConstructionSiteInfoStaff ngConstructionSiteInfoStaff : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("人员名称", ngConstructionSiteInfoStaff.getStaffName());
            map.put("人员职位", ngConstructionSiteInfoStaff.getStaffPosition());
            map.put("人员照片", ngConstructionSiteInfoStaff.getStaffPhotoPath());
            map.put("打卡次数", ngConstructionSiteInfoStaff.getStaffSignIn());
            map.put("创建者", ngConstructionSiteInfoStaff.getCreateBy());
            map.put("更新者", ngConstructionSiteInfoStaff.getUpdateBy());
            map.put("创建日期", ngConstructionSiteInfoStaff.getCreateTime());
            map.put("更新时间", ngConstructionSiteInfoStaff.getUpdateTime());
            map.put(" siteId",  ngConstructionSiteInfoStaff.getSiteId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Map<String, Object>> getStaffW(int siteId) {
        return ngConstructionSiteInfoStaffMapper.getStaffW(siteId);
    }

    @Override
    public List<Map<String, Object>> getStaffY(int siteId) {
        return ngConstructionSiteInfoStaffMapper.getStaffY(siteId);
    }

    @Override
    public void saveStaff(Object data){
        Map dataMap = JSON.parseObject(JSON.toJSONString(data), Map.class);
        List<Integer> personIds = JSON.parseArray(JSON.toJSONString(dataMap.get("personIds")), Integer.class);
        Object siteId = dataMap.get("id");
        ngConstructionSiteInfoStaffMapper.deleteBySiteId(Integer.parseInt(siteId.toString()));
        for(Integer staffId:personIds){
            NgConstructionSiteInfoStaff vo = new NgConstructionSiteInfoStaff();
            vo.setSiteId(Integer.parseInt(siteId.toString()));
            vo.setStaffUserId(staffId);
            save(vo);
        }
    }
}